package com.ruoyi.college.controller;

import java.util.List;

import com.ruoyi.college.domain.Course;
import com.ruoyi.college.domain.CourseArrangement;
import com.ruoyi.college.service.ICourseArrangementService;
import com.ruoyi.college.service.ICourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.ExperimentScoreStandard;
import com.ruoyi.college.service.IExperimentScoreStandardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 设置评分标准Controller
 * 
 * @author ruoyi
 * @date 2025-08-27
 */
@Controller
@RequestMapping("/college/scoreStandard")
public class ExperimentScoreStandardController extends BaseController
{
    private String prefix = "college/scoreStandard";

    @Autowired
    private IExperimentScoreStandardService experimentScoreStandardService;

    @Autowired
    private ICourseArrangementService  courseArrangementService;

    @Autowired
    private ICourseService courseService;

    @RequiresPermissions("college:scoreStandard:view")
    @GetMapping()
    public String scoreStandard(ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        return prefix + "/scoreStandard";
    }

    /**
     * 查询设置评分标准列表
     */
    @RequiresPermissions("college:scoreStandard:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExperimentScoreStandard experimentScoreStandard)
    {
        startPage();
        List<ExperimentScoreStandard> list = experimentScoreStandardService.selectExperimentScoreStandardList(experimentScoreStandard);
        return getDataTable(list);
    }

    /**
     * 导出设置评分标准列表
     */
    @RequiresPermissions("college:scoreStandard:export")
    @Log(title = "设置评分标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExperimentScoreStandard experimentScoreStandard)
    {
        List<ExperimentScoreStandard> list = experimentScoreStandardService.selectExperimentScoreStandardList(experimentScoreStandard);
        ExcelUtil<ExperimentScoreStandard> util = new ExcelUtil<ExperimentScoreStandard>(ExperimentScoreStandard.class);
        return util.exportExcel(list, "设置评分标准数据");
    }

    /**
     * 新增设置评分标准
     */
    @RequiresPermissions("college:scoreStandard:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        return prefix + "/add";
    }

    /**
     * 新增保存设置评分标准
     */
    @RequiresPermissions("college:scoreStandard:add")
    @Log(title = "设置评分标准", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExperimentScoreStandard experimentScoreStandard)
    {
        return toAjax(experimentScoreStandardService.insertExperimentScoreStandard(experimentScoreStandard));
    }

    /**
     * 修改设置评分标准
     */
    @RequiresPermissions("college:scoreStandard:edit")
    @GetMapping("/edit/{standardId}")
    public String edit(@PathVariable("standardId") Long standardId, ModelMap mmap)
    {
        List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        mmap.put("courseArrangements", courseArrangements);
        ExperimentScoreStandard experimentScoreStandard = experimentScoreStandardService.selectExperimentScoreStandardByStandardId(standardId);
        //mmap.put("experimentScoreStandard", experimentScoreStandard);
        mmap.put("scoreStandard", experimentScoreStandard);
        return prefix + "/edit";
    }



    @RequiresPermissions("college:scoreStandard:edit")
    @GetMapping("/editOne/{standardId}")
    public String editOne(@PathVariable("standardId") Long standardId, ModelMap mmap)
    {
        //List<CourseArrangement> courseArrangements = courseArrangementService.selectCourseArrangementList(new CourseArrangement());
        //mmap.put("courseArrangements", courseArrangements);
        ExperimentScoreStandard experimentScoreStandard = experimentScoreStandardService.selectExperimentScoreStandardByStandardId(standardId);
        //mmap.put("experimentScoreStandard", experimentScoreStandard);
        Long arrangementId = experimentScoreStandard.getArrangementId();
        CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
        Course course = courseService.selectCourseByCourseId(courseArrangement.getCourseId());
        mmap.put("course", course);
        experimentScoreStandard.setCourseId(courseArrangement.getCourseId());
        experimentScoreStandard.setCourseName(courseArrangement.getCourseName());
        mmap.put("scoreStandard", experimentScoreStandard);

        return prefix + "/editOne";
    }

    /**
     * 修改保存设置评分标准
     */
    @RequiresPermissions("college:scoreStandard:edit")
    @Log(title = "设置评分标准", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExperimentScoreStandard experimentScoreStandard)
    {
        return toAjax(experimentScoreStandardService.updateExperimentScoreStandard(experimentScoreStandard));
    }

    /**
     * 删除设置评分标准
     */
    @RequiresPermissions("college:scoreStandard:remove")
    @Log(title = "设置评分标准", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(experimentScoreStandardService.deleteExperimentScoreStandardByStandardIds(ids));
    }

    /**
     * 保存评分标准
     */
    @PostMapping("/save")
    @RequiresPermissions("college:scoreStandard:remove")
    @ResponseBody
    public AjaxResult save(@RequestBody ExperimentScoreStandard scoreStandard) {
        return toAjax(experimentScoreStandardService.saveScoreStandard(scoreStandard));
    }

    /**
     * 删除评分标准
     */
    @PostMapping("/remove/{id}")
    @RequiresPermissions("college:scoreStandard:remove")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(experimentScoreStandardService.deleteScoreStandard(id));
    }

    /**
     * 导出评分标准列表
     */
    //@PostMapping("/export")
    //@RequiresPermissions("college:scoreStandard:remove")
    public void export(HttpServletResponse response) {
        List<ExperimentScoreStandard> list = experimentScoreStandardService.selectByLeader();
        ExcelUtil<ExperimentScoreStandard> util = new ExcelUtil<>(ExperimentScoreStandard.class);
        util.exportExcel(response, list, "实验评分标准列表");
    }
}
